//
//  HEAssesListView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/13.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEAssesListView.h"
#import "HEMoreView.h"
#import "HEAssesListModel.h"
#import "HEAssesCell.h"
#import "HEHealthAssesViewController.h"

@interface HEAssesListView ()<UITableViewDelegate, UITableViewDataSource, HEAssesCellDelegate>

@property (nonatomic, copy) NSMutableArray<HEAssesListModel *> *dataSourceArray;

@end

@implementation HEAssesListView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        self.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
        self.layer.shadowOffset = CGSizeMake(0,5);
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 6;
        self.layer.cornerRadius = 10;
                
        [self initUI];
    }
    return self;
}

- (NSMutableArray *)dataSourceArray {
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)initUI {
    
    HEMoreView *allView = [[HEMoreView alloc] init];
    allView.title = @"健康评估";
    [self addSubview:allView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toHealthAssesClick)];
    [allView addGestureRecognizer:tap];
    
    allView.sd_layout
    .topSpaceToView(self, 13)
    .leftSpaceToView(self, 15)
    .rightSpaceToView(self, 15)
    .heightIs(20);

    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    [self addSubview:_tableView];

    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HEAssesCell class]) bundle:nil] forCellReuseIdentifier:@"HEAssesCell"];

    _tableView.sd_layout
    .topSpaceToView(allView, 19)
    .leftSpaceToView(self, 5)
    .rightSpaceToView(self, 5)
    .bottomSpaceToView(self, 5);
    
    [self loadData];
}

- (void)loadData {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KScaleInfoUrl parameters:@{} success:^(id  _Nonnull responseObject) {
       
        [self.dataSourceArray removeAllObjects];
        
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArray addObjectsFromArray:[HEAssesListModel mj_objectArrayWithKeyValuesArray:response.data]];
        
        if (self.dataSourceArray.count > 4) {
            [self.dataSourceArray removeObjectsInRange:NSMakeRange(4, self.dataSourceArray.count - 4)];
        }
        
        [self.tableView reloadData];
        
        if (self.heightBlock) {
            self.heightBlock(20 + 13 + 19 + 52 * self.dataSourceArray.count + 5);
        }
    } failure:^(NSError * _Nonnull error) {
    }];
}
 
- (void)getTableHeight:(returnTableHeight)block {
    self.heightBlock = block;
}

- (void)toHealthAssesClick {
    HEHealthAssesViewController *vc = [HEHealthAssesViewController new];
    [self.superVC.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEAssesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HEAssesCell"];
    cell.model = self.dataSourceArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 52;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEAssesListModel *model = _dataSourceArray[indexPath.row];
}

- (void)toCheckVC:(HEAssesListModel *)model {
    NSBundle *bundle = [NSBundle mainBundle];
        NSString *resPath = [bundle resourcePath];
        NSString *filePath = [resPath stringByAppendingPathComponent:@"/H5/form/index.html"];
    NSURL * file = [NSURL fileURLWithPath:filePath];
  
    NSString *loclUrl = [NSString stringWithFormat:@"%@?pagetype=1&questiontype=%@&token=%@&height=%f&url=%@/oldman&reference=%@",file.absoluteURL, model.check_code,[UserManager sharedUserManager].getToken, kNavBarHeight,KServerUrl, [model.check_reference stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
    XLWebViewController *webView = [[XLWebViewController alloc] init];
    webView.url = loclUrl;
    [webView showNav:NO];
    [self.superVC.navigationController pushViewController:webView animated:YES];
}

#pragma mark - UITableViewDelegate

@end
